package com.ziyun.erp.web.modules.capital.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ziyun.erp.common.utils.UserUtils;
import com.ziyun.erp.modules.capital.entity.MerchantApplyCashEntity;
import com.ziyun.erp.modules.capital.entity.MerchantApplyCashItemsEntity;
import com.ziyun.erp.modules.capital.entity.MerchantApplyLogEntity;
import com.ziyun.erp.modules.capital.service.MerchantApplyCashItemsService;
import com.ziyun.erp.modules.capital.service.MerchantApplyCashService;
import com.ziyun.erp.modules.capital.service.MerchantApplyLogService;
import com.ziyun.erp.modules.capital.vo.MerchantApplyCashItemsVo;
import com.ziyun.erp.modules.micro.service.MicroService;
import com.ziyun.erp.modules.supp.entity.SellerInfoEntity;
import com.ziyun.erp.modules.supp.service.SellerInfoService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.sys.service.SysUserService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zh
 * @email 296588519@qq.com
 * @date 2018-08-03 10:29:41
 */
@RestController
@RequestMapping("capital/merchantapplycash")
public class MerchantApplyCashController {
	@Autowired
	private MerchantApplyCashService merchantApplyCashService;

	@Autowired
	private MerchantApplyCashItemsService merchantApplyCashItemsService;

	@Autowired
	private MerchantApplyLogService merchantApplyLogService;

	@Autowired
	private MicroService microService;

	@Autowired
	private SellerInfoService sellerInfoService;

	private static Logger logger = LoggerFactory.getLogger(MerchantApplyCashController.class);


	/**
	 * 小b审核提现列表
	 */
	@RequestMapping("/smallBCheckList")
	// @RequiresPermissions("capital:merchantapplycash:list")
	public R smallBCheckList(@RequestParam Map<String, Object> params) {
		params.put("merchantType",0);
		PageUtils page = merchantApplyCashService.queryPage(params);
		return R.ok().put("page", page);
	}

	/**
	 * 工厂审核提现列表
	 */
	@RequestMapping("/factoryCheckList")
	// @RequiresPermissions("capital:merchantapplycash:list")
	public R factoryCheckList(@RequestBody Map<String, Object> params) {
		params.put("merchantType",1);
		PageUtils page = merchantApplyCashService.queryPage(params);
		return R.ok().put("page", page);
	}


	@RequestMapping("/getSellerInfo")
	public R getSellerInfo(@RequestParam String keyWord, String sellerType ) {
		List<SellerInfoEntity> sellerList = this.sellerInfoService.selectList(new EntityWrapper<SellerInfoEntity>()
										.eq("seller_type",sellerType )
										.like("seller_name",keyWord )
		);
		return R.ok().put("sellerList",sellerList);
	}

	/**
	 * 小b 提现审核
	 *
	 * @param id
	 * @return
	 */
	@RequestMapping("/checkSmallB")
	public R checkSmallB(@RequestBody Integer id) {
		String rs = merchantApplyCashService.applyCashCheck(UserUtils.getCurentUser().getUsername(),id);
		return R.ok(rs);
	}

	/**
	 * 工厂 提现审核
	 *
	 * @param id
	 * @return
	 */
	@RequestMapping("/checkFactory")
	public R checkFactory(@RequestBody Integer id) {
		String rs = merchantApplyCashService.applyCashCheck(UserUtils.getCurentUser().getUsername(),id);
		return R.ok(rs);
	}

	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	// @RequiresPermissions("capital:merchantapplycash:info")
	public R info(@PathVariable("id") Integer id) {
		// 请求主表信息
		MerchantApplyCashEntity merchantApplyCash = merchantApplyCashService.selectApplyCashById(id);

		// 请求明细
		List<MerchantApplyCashItemsVo> merchantApplyCashItemsList = merchantApplyCashItemsService
				.selectApplyCashItemList(id);

		// 请求原始单据明细
		List<MerchantApplyCashItemsEntity> oldMerchantApplyCashItemsList = merchantApplyCashItemsService
				.selectOldApplyCashItemList(id);

		// 请求日志
		List<MerchantApplyLogEntity> merchantApplyLogList = merchantApplyLogService
				.selectList(new EntityWrapper<MerchantApplyLogEntity>().eq("apply_cash_id", id));

		R r = R.ok();
		r.put("merchantApplyCash", merchantApplyCash);
		r.put("merchantApplyCashItems", merchantApplyCashItemsList);
		r.put("oldMerchantApplyCashItems", oldMerchantApplyCashItemsList);
		r.put("merchantApplyLog", merchantApplyLogList);

		return r;
	}

	/**
	 * 联动支付回调
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "notifyUrl")
	public void getNotifyInfo(HttpServletRequest request, HttpServletResponse response){
		Map<String, Object> map = new HashMap<>();
		//获取联动发送请求的参数
		String requestParam =  request.getQueryString();
		System.out.println("请求参数 :" + requestParam);
		//requestParam = "amount=1&com_amt=50&com_amt_type=-99&mer_date=20180531&mer_id=50956&order_id=AP-1805311539602667&ret_code=0000&ret_msg=%E4%BA%A4%E6%98%93%E6%88%90%E5%8A%9F&trade_no=1805311539174094&trade_state=4&transfer_date=20180531&transfer_settle_date=20180531&version=4.0&sign=gwiYYlbBqFAQt40%2B%2BHIrxhaAcyzQ20TSHqHChbQ58dwWHzbERec0tQ%2B1Bwq8m13DrvsGzGW7%2BpqUlMQHD4ce0fppgnxf%2FDhlNYgftxfx5mtfcYVZmHhJvrwkw4BNZ4O5ZL0MUAyD070Oa23nOPk47ZKO3pmop2sr72L2oPZml4Y%3D&sign_type=RSA";
		map.put("params",requestParam);
		String res = merchantApplyCashService.getNotifyInfo(map);
		if(StringUtils.isNotBlank(res)){
			try {
				response.setContentType("text/html;charset=utf-8");
				PrintWriter out = response.getWriter();
				out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
				out.println("<HTML>");
				out.println(" <HEAD><META NAME=\"MobilePayPlatform\" CONTENT=\"" + res + "\" /></HEAD>");
				out.println(" <BODY>");
				out.print(" This is ");
				out.print(this.getClass());
				out.println(", using the GET method");
				out.println("</BODY>");
				out.println("</HTML>");
				out.flush();
				out.close();
			}catch (Exception e){
				logger.error("返回数据到联动平台异常！" +e.getMessage());
				e.printStackTrace();
			}
		}
	}

	/**
	 * 联动订单查看
	 * @param queryParams
	 * @return
	 */
	@RequestMapping(value = "searchUmfOrderStatus")
	public R searchUmfOrderStatus(@RequestBody Map<String,Object> queryParams){
		return  R.ok().put("data",merchantApplyCashService.searchUmfOrderStatus(queryParams)) ;
	}

	/**
	 * 确认联动已打款
	 * @param queryParams
	 * @return
	 */
	//@RequiresPermissions("capital:merchantapplycash:admin")
	@RequestMapping(value = "confirmCash")
	public R confirmCash(@RequestBody Map<String,Object> queryParams){
		merchantApplyCashService.confirmCash(queryParams);
		return  R.ok("确认已打款成功");
	}

	/**
	 * 重置打款状态
	 * @param queryParams
	 * @return
	 */
	//@RequiresPermissions("capital:merchantapplycash:admin")
	@RequestMapping(value = "resetApplyStatus")
	public R resetApplyStatus(@RequestBody Map<String,Object> queryParams){
		merchantApplyCashService.resetApplyStatus(queryParams);
		return  R.ok("重置状态成功");
	}


}
